<?php

namespace App\Http\Middleware\Auth;

use App\Models\ApiResult;
use App\Models\User;
use App\Services\Base\CacheService;
use App\Services\Base\MsgService;
use Closure;
use Illuminate\Http\Request;

class CheckTradeStatus
{
    /**
     * 交易状态检测
     * @param Request $request
     * @param Closure $next
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->bearerToken();
        $user = CacheService::hGet(R_LOGIN_TOKEN_INFO, $token, true);
        if (!$user) {
            return response()->json(ApiResult::failure(401, MsgService::toMessage(401)));
        }

        /**
         * @var $user User
         */
        $user = User::query()->find($user['uid']);
        if ($user->trade_status == 0) {
            return response()->json(ApiResult::failure(500, __('账号交易锁定，请联系客服')));
        }

        return $next($request);
    }
}
